//
//  PayInputView.m
//  SecureTextEntryView
//
//  Created by 杨林杰 on 2019/5/18.
//  Copyright © 2019 YLJ. All rights reserved.
//

#import "PayInputView.h"

@interface PayInputView ()<SecureTextEntryDelegate>

/**
 输入框白色背景View
 */
@property (nonatomic, strong) UIView    *bgWhiteView;
/**
 透明背景View
 */
@property (nonatomic, strong) UIView    *bgView;
/**
 描述文字Label
 */
@property (nonatomic, strong) UILabel   *describeLabel;
/**
 取消Button
 */
@property (nonatomic, strong) UIButton  *cancelButton;
/**
 安全输入框
 */
@property (nonatomic, strong) SecureTextEntryView *secureView;
/**
 屏幕尺寸
 */
@property (nonatomic, assign) CGSize    screenSize;
/**
 屏幕宽度比例
 */
@property (nonatomic, assign) CGFloat   widthScale;
/**
 输入框白色背景高度
 */
@property (nonatomic, assign) CGFloat   bgWhiteViewHeight;
/**
 描述文字高度
 */
@property (nonatomic, assign) CGFloat   describeHeight;
/**
 输入框高度
 */
@property (nonatomic, assign) CGFloat   inputHeight;
/**
 描述文字颜色
 */
@property (nonatomic, strong) UIColor   *describeColor;
@end

@implementation PayInputView
// MARK: 初始化
static PayInputView *instance = nil;

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[PayInputView alloc] init];
    });
    return instance;
}
- (instancetype)init {
    self = [super init];
    if (self) {
        self.frame = [UIScreen mainScreen].bounds;
        self.screenSize = [UIScreen mainScreen].bounds.size;
        self.widthScale = self.screenSize.width / 375.0;
        self.inputHeight = 50.0;
        self.describeHeight = 52.0 * self.widthScale;
        self.bgWhiteViewHeight = 147.0 * self.widthScale;
        self.describeColor = [UIColor blackColor];
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
        self.alpha = 0.0;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShowOrHide:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShowOrHide:) name:UIKeyboardWillHideNotification object:nil];
        
        [self addSubview:self.bgView];
        [self addSubview:self.bgWditeView];
        [self.bgWditeView addSubview:self.describeLabel];
        [self.bgWditeView addSubview:self.cancelButton];
        [self.bgWditeView addSubview:self.secureView];
        
        __weak typeof(self) weakSelf = self;
        [self.secureView updateConfig:^(SecureTextEntryViewConfig * _Nonnull config) {
            __strong typeof(weakSelf) self = weakSelf;
            config.inputWidth = self.inputHeight;
        }];
    }
    return self;
}
// MARK: 公有方法
- (void)display {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.alpha = 0.0;
    [self.secureView becomeFirstResponder];
    [self.secureView reset];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1.0;
    }];
}
- (void)unDisplay {
    self.alpha = 1.0;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0.0;
        [self.secureView resignFirstResponder];
    } completion:^(BOOL finished) {
        [[UIApplication sharedApplication].keyWindow removeFromSuperview];
    }];
}
// MARK: KVO
- (void)keyBoardShowOrHide:(NSNotification *)noti {
    NSDictionary *userinfo = noti.userInfo;
    NSValue *value = (NSValue *)userinfo[UIKeyboardFrameEndUserInfoKey];
    CGFloat keyboardHeight = 0.0;
    if ([noti.name isEqualToString:UIKeyboardWillShowNotification]) {
        keyboardHeight = value.CGRectValue.size.height;
    } else {
        keyboardHeight = 0.0;
    }
    self.bgWditeView.frame = CGRectMake(0.0, self.screenSize.height - keyboardHeight - self.bgWhiteViewHeight, self.screenSize.width, self.bgWhiteViewHeight);
}
// MARK: 私有方法
- (void)handleBgTap:(UITapGestureRecognizer *)sender {
    [self unDisplay];
}
- (void)handleCancelButton:(UIButton *)sender {
    [self unDisplay];
}
// MARK: 安全输入框协议
- (void)secureTextEntryBegin:(SecureTextEntryView *)secureTextEntryView {
    
}
- (void)secureTextEntryDidChange:(SecureTextEntryView *)secureTextEntryView {
    
}
- (void)secureTextEntryDidDelete:(SecureTextEntryView *)secureTextEntryView {
    
}
- (void)secureTextEntryComplete:(SecureTextEntryView *)secureTextEntryView {
    if (self.inputCompleteBlock) {
        self.inputCompleteBlock(secureTextEntryView);
    }
}
- (void)secureTextEntryEnd:(SecureTextEntryView *)secureTextEntryView {
    
}
// MARK: UI
- (UIButton *)cancelButton {
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelButton.frame = CGRectMake(self.screenSize.width - 73.0, 0.0, 60.0, self.describeHeight);
        _cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _cancelButton.titleLabel.font = [UIFont systemFontOfSize:15.0 * self.widthScale];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:self.describeColor forState:UIControlStateNormal];
        [_cancelButton addTarget:self action:@selector(handleCancelButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}
- (UILabel *)describeLabel {
    if (!_describeLabel) {
        _describeLabel = [[UILabel alloc] initWithFrame:CGRectMake(13.0, 0.0, 150.0, self.describeHeight)];
        _describeLabel.textAlignment = NSTextAlignmentLeft;
        _describeLabel.font = [UIFont systemFontOfSize:15.0 * self.widthScale];
        _describeLabel.textColor = self.describeColor;
        _describeLabel.text = @"请输入支付密码";
    }
    return _describeLabel;
}
- (SecureTextEntryView *)secureView {
    if (!_secureView) {
        CGFloat gap = (self.bgWhiteViewHeight - self.describeHeight - self.inputHeight) / 2.0;
        CGFloat y = self.bgWhiteViewHeight - self.describeHeight - gap;
        _secureView = [[SecureTextEntryView alloc] initWithFrame:CGRectMake(0.0, y, self.screenSize.width, self.inputHeight)];
        _secureView.delegate = self;
    }
    return _secureView;
}
- (UIView *)bgWditeView {
    if (!_bgWhiteView) {
        _bgWhiteView = [[UIView alloc] initWithFrame:CGRectMake(0.0, self.screenSize.height, self.screenSize.width, self.bgWhiteViewHeight)];
        _bgWhiteView.backgroundColor = [UIColor whiteColor];
    }
    return _bgWhiteView;
    
}
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:self.bounds];
        _bgView.backgroundColor = [UIColor clearColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleBgTap:)];
        [_bgView addGestureRecognizer:tap];
    }
    return _bgView;
}
@end
